//
//  ReceiveCallHeaderView.m
//  cloudCall
//
//  Created by zhang on 2021/11/2.
//

#import "ReceiveCallHeaderView.h"
#import <CloudCallBaseFrame/CommonFrame.h>
#import "QTBClientCallCore.h"
#import "LCPatientModel.h"


@interface ReceiveCallHeaderView ()

/**头像*/
@property (nonatomic, strong)UIImageView *imageView;
/**名称*/
@property (nonatomic, strong)UILabel *nameLabel;
/**接听*/
@property (nonatomic, strong)UIButton *acceptBtn;
/**挂断*/
@property (nonatomic, strong)UIButton *hangupBtn;

@property (nonatomic, strong)LCPatientModel *currentPatient;

@end


@implementation ReceiveCallHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:CGRectMake(8, AutoStateHeight+15, kScreenWidth-16, 88)];
    if (self) {
        [self setUpUI];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidEnd) name:CallingDidEndNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidError) name:CallingDidErrorNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callingDidConnect) name:CallingDidConnectNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPatientsInfo) name:GetPatientsInfoNotification object:nil];
    }
    return self;
}


-(void)setUpUI{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
    [self addGestureRecognizer:tap];
    self.layer.cornerRadius = 10;
    self.layer.masksToBounds = YES;
    self.backgroundColor = [UIColor colorWithHexString:@"#000000" withAlpha:0.5];
    self.imageView = [[UIImageView alloc] init];
    self.imageView.image = [UIImage imageNamed:@"ImageSource.bundle/user_default@3x"];
    [self addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.top.left.offset(15);
    }];
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
    self.nameLabel.font = [UIFont systemFontOfSize:24 weight:(UIFontWeightMedium)];
    [self addSubview: self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.imageView.mas_centerY);
        make.left.equalTo(self.imageView.mas_right).offset(8);
    }];
    
    UILabel *noteLabel = [[UILabel alloc] init];
    noteLabel.text = @"邀请你进行语言通话";
    noteLabel.textColor = [UIColor colorWithHexString:@"#FFFFFF" withAlpha:0.7];
    noteLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:noteLabel];
    [noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(-15);
        make.left.offset(15);
    }];
    
    self.acceptBtn = [[UIButton alloc] init];
    [self addSubview:self.acceptBtn];
    [self.acceptBtn setBackgroundImage:[UIImage imageNamed:@"ImageSource.bundle/calling@3x"] forState:(UIControlStateNormal)];
    [self.acceptBtn addTarget:self action:@selector(acceptAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.acceptBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(44, 44));
        make.right.offset(-74);
        make.centerY.offset(0);
    }];
    
    self.hangupBtn = [[UIButton alloc] init];
    [self addSubview:self.hangupBtn];
    [self.hangupBtn addTarget:self action:@selector(hangupAction) forControlEvents:(UIControlEventTouchUpInside)];
    [self.hangupBtn setBackgroundImage:[UIImage imageNamed:@"ImageSource.bundle/hangup@3x"] forState:(UIControlStateNormal)];
    [self.hangupBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(44, 44));
        make.right.offset(-15);
        make.centerY.offset(0);
    }];
}

-(void)getPatientsInfo{
    if ([WWPortManagerTool defaultTool].patientsArray && [WWPortManagerTool defaultTool].patientsArray.count) {
        LCPatientModel *model = [WWPortManagerTool defaultTool].patientsArray[0];
        self.currentPatient = model;
    }
}

-(void)setCurrentPatient:(LCPatientModel *)currentPatient {
    _currentPatient = currentPatient;
    self.nameLabel.text = currentPatient.name;
    UIImage *holder = [UIImage imageNamed:@"ImageSource.bundle/user_default@3x"];
    if ([currentPatient.gender isEqualToString:@"M"]) {
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_man@3x"];
    } else if ([currentPatient.gender isEqualToString:@"F"]) {
        holder = [UIImage imageNamed:@"ImageSource.bundle/user_woman@3x"];
    }
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:currentPatient.avatarUrl] placeholderImage:holder];
    [[NSNotificationCenter defaultCenter] postNotificationName:SetCurrentPatientNotification object:currentPatient];
}

-(void)setAcount:(NSString *)acount {
    _acount = acount;
    self.nameLabel.text = acount;
}

-(void)tapAction{
    if (self.clickBlock) {
        self.clickBlock();
    }
    [self removeFromSuperview];
}

-(void)showView{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
}


-(void)acceptAction{
    [[QTBClientCallCore shareCallCore] answerCall];
}

-(void)hangupAction{
    [[QTBClientCallCore shareCallCore] sendOnHook];
}

-(void)callingDidEnd{
    [self removeFromSuperview];
}


-(void)callingDidError{
    [self removeFromSuperview];
}


-(void)callingDidConnect{
    [self tapAction];
}


-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
